package com.jianyun.jyzs.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.TradeDetailAdapter;
import com.jianyun.jyzs.bean.EngineerProjectResult;
import com.jianyun.jyzs.dao.EngProjectDao;
import com.jianyun.jyzs.fragment.P1FragmentAllFragment;
import com.jianyun.jyzs.fragment.P1FragmentCommon;
import com.jianyun.jyzs.fragment.P2FragmentAllFragment;
import com.jianyun.jyzs.fragment.P2FragmentCommon;
import com.jianyun.jyzs.model.EngineerModel;
import com.jianyun.jyzs.model.imdoel.IEngineerModel;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jrmf360.rylib.common.util.ScreenUtil;
import com.jrmf360.rylib.common.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yangshijun on 2018/5/31.
 * 选择工程动态项目界面
 */

public class SelectEProjectActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.tv_checked)
    TextView tvChecked;
    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.iv_moreLinearLayout)
    LinearLayout ivMoreLinearLayout;
    @BindView(R.id.topCreat)
    LinearLayout topCreat;
    @BindView(R.id.iv_single)
    ImageView ivSingle;
    @BindView(R.id.iv_group)
    ImageView ivGroup;
    @BindView(R.id.topChatDetail)
    LinearLayout topChatDetail;
    @BindView(R.id.topId)
    RelativeLayout topId;
    @BindView(R.id.tv_in)
    TextView tvIn;
    @BindView(R.id.ll_in)
    LinearLayout llIn;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.ll_all)
    LinearLayout llAll;
    @BindView(R.id.id_switch_tab_ll)
    LinearLayout idSwitchTabLl;
    @BindView(R.id.id_tab_line_iv)
    ImageView idTabLineIv;
    @BindView(R.id.del)
    ImageView del;
    @BindView(R.id.ll_notice)
    LinearLayout llNotice;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private TradeDetailAdapter tradeDetailAdapter;
    private List<Fragment> mFragmentList = new ArrayList();
    private List<Fragment> mFragmentList2 = new ArrayList();
    private int lineWidth;
    private FragmentManager mFm;
    public String enterpriseCode;
    private P1FragmentAllFragment p1FragmentAll;
    private P1FragmentCommon p1FragmentCommon;

    boolean isNext = false;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_e_select_p1);
        ButterKnife.bind(this);
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        initData();
        initView();
        initDao();
    }
    private void initView() {
        mFm = getSupportFragmentManager();
        tradeDetailAdapter = new TradeDetailAdapter(mFm, mFragmentList);
        viewPager.setAdapter(tradeDetailAdapter);
        topBack.setVisibility(View.VISIBLE);
        topText.setText("选择项目");
        int var1 = ScreenUtil.getScreenWidth(this);
        LinearLayout.LayoutParams var2 = (LinearLayout.LayoutParams) this.idTabLineIv.getLayoutParams();
        lineWidth = var2.width = var1 / this.mFragmentList.size();
        idTabLineIv.setLayoutParams(var2);
        viewPager.setOnPageChangeListener(this);
    }

    private void initData() {
        p1FragmentAll = new P1FragmentAllFragment(this);
        p1FragmentCommon = new P1FragmentCommon(this);
        mFragmentList.add(p1FragmentCommon);
        mFragmentList.add(p1FragmentAll);
    }

    private void initDao() {
        EngProjectDao dao = new EngProjectDao(this);
        HashMap<String, String> map = new HashMap<>();
        map.put("enterpriseCode", enterpriseCode);
        map.put("method", "GetProJectList");
        map.put("pageIndex", "1");
        List<EngineerProjectResult.EngProject> engineer = dao.getEngineer(enterpriseCode);
        if (engineer.size() > 0) {
            tradeDetailAdapter.setFragmentList(mFragmentList);
        } else {
            EngineerModel.getInstance().getP1Data(dao, map, new IEngineerModel.OnP1DataListener() {
                @Override
                public void onP1Success() {
                    tradeDetailAdapter.setFragmentList(mFragmentList);
                }

                @Override
                public void onP1Failed(String msg) {
                    ToastUtil.showNoWaitToast(SelectEProjectActivity.this, msg);
                }
            });

        }
    }

    @Override
    public void onBackPressed() {
        if (isNext) {
            setLastLevelView();
        } else {
            finish();
        }
    }

    @OnClick({R.id.topBack, R.id.ll_in, R.id.ll_all, R.id.del})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.topBack:
                finish();
                break;
            case R.id.ll_all:
                viewPager.setCurrentItem(1);
                break;
            case R.id.ll_in:
                viewPager.setCurrentItem(0);
                break;
            case R.id.del:
                llNotice.setVisibility(View.GONE);
                break;
        }
    }

    public void setNextLevelView(EngineerProjectResult.EngProject project) {
        topText.setText("选择部位");
        tvAll.setText("全部部位");
        tvIn.setText("常用部位");
        isNext = true;

        mFragmentList2.clear();
        mFragmentList2.add(new P2FragmentCommon(project));
        mFragmentList2.add(new P2FragmentAllFragment(project));
        viewPager.setAdapter(new TradeDetailAdapter(getSupportFragmentManager(), mFragmentList2));

    }

    public void setLastLevelView() {
        topText.setText("选择项目");
        tvAll.setText("全部项目");
        tvIn.setText("常用项目");
        isNext = false;
        mFragmentList.clear();
        mFragmentList.add(new P1FragmentCommon(this));
        mFragmentList.add(new P1FragmentAllFragment(this));
        viewPager.setAdapter(new TradeDetailAdapter(getSupportFragmentManager(), mFragmentList));
    }

    @Override
    public void onPageScrolled(int var1, float var2, int var3) {
        LinearLayout.LayoutParams var4 = (LinearLayout.LayoutParams) idTabLineIv.getLayoutParams();
        int var5 = this.lineWidth * var1 + var3 / this.mFragmentList.size();
        var4.leftMargin = var5;
        idTabLineIv.setLayoutParams(var4);
    }

    @Override
    public void onPageSelected(int var1) {
        this.resetTextView();
        if (var1 == 0) {
            tvIn.setTextColor(this.getResources().getColor(R.color.blue));
        } else if (var1 == 1) {
            tvAll.setTextColor(this.getResources().getColor(R.color.blue));
        }
    }

    private void resetTextView() {
        tvAll.setTextColor(this.getResources().getColor(R.color.color_959595));
        tvIn.setTextColor(this.getResources().getColor(R.color.color_959595));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
